<?php
namespace app\admin\controller;

use app\common\model\admin\AccountModel;
use app\common\model\admin\AccountStoreModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use app\common\model\shop\ShopsModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;

/**
 * 关联仓库列表
 * @class User
 * @package app\admin\controller
 */
class Store extends Controller
{
    public $check_user = false;

    /**
     * 关联仓库列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {
        AccountStoreModel::mQuery()->layTable(function () {

            $this->title = '合作仓库列表';

            $this->shop_arr = ShopsModel::mk()->where('account_id', $this->account_id)->column('name', 'id');

            $this->relation_account_arr = AccountModel::mk()->whereRaw('find_in_set("'.AccountModel::MODULE_LOGICS.'", module)')->column('title', 'id');

            //$this->warehouse_arr = [];

        }, function (QueryHelper $query) {

            // 数据列表搜索过滤
            $query->equal('account_id,shop_id,relation_account_id,warehouse_id');
        });
    }


    protected function _index_page_filter (&$data)
    {
        $shop_ids      = array_column($data, 'shop_id');
        $warehouse_ids = array_column($data, 'warehouse_id');

        $account_arr   = AccountModel::mk()->where('1=1')->column('title', 'id');
        $shop_arr      = ShopsModel::mk()->whereIn('id', $shop_ids)->column('name', 'id');
        $warehoser_arr = WarehouseModel::mk()->whereIn('id', $warehouse_ids)->column('warehouse_c_name', 'id');

        foreach ( $data as &$item) {


            $item['account_name']           = $account_arr[$item['account_id']] ?? '';
            $item['shop_name']              = $shop_arr[$item['shop_id']]  ?? '';
            $item['relation_account_name']  = $account_arr[$item['relation_account_id']]  ?? '';
            $item['warehouse_name']         = $warehoser_arr[$item['warehouse_id']]  ?? '';

        }
    }

    /**
     * 添加系统用户
     * @auth true
     */
    public function add()
    {
        AccountStoreModel::mForm();
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {
        AccountStoreModel::mDelete();
    }

    protected function _form_filter (&$data)
    {
        if ( $this->request->isGet() ) {
            // 店铺
            $account_id = $this->super ? reset($this->account_arr) : $this->account_id;

            $this->shop_arr = ShopsModel::mk()->where('account_id', $account_id)->column('name', 'id');

            // 仓库公司
            $this->store_account_arr = AccountModel::mk()->whereRaw('find_in_set("'.AccountModel::MODULE_LOGICS.'", module)')->column('title', 'id');

            $account_id = reset($this->store_account_arr);
            $this->warehouse_arr = WarehouseModel::mk()->where('account_id', $account_id)->column('warehouse_c_name', 'id');

        }
    }
    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {

        AccountStoreModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }


}

?>